package top.infopub.mgr.bpa.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.ProContractAttDao;
import top.infopub.mgr.bpa.dao.ProContractDao;
import top.infopub.mgr.bpa.dao.ProContractDetailDao;
import top.infopub.mgr.bpa.dao.ProContractInvestDao;
import top.infopub.mgr.bpa.dao.ProContractSubDao;
import top.infopub.mgr.bpa.dao.ProContractUlaDao;
import top.infopub.mgr.bpa.domain.ProContractDomain;
import top.infopub.mgr.bpa.service.ProContractService;
import top.infopub.mgr.bpa.vo.ProContractVo;
import top.infopub.mgr.bpast.domain.BpastContractDomain;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.hr.dao.OrgInfoDao;
import top.infopub.mgr.sg.dao.CitySurveyDao;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.bpa.ProContract;
import top.infopub.model.bpa.ProContractAtt;
import top.infopub.model.bpa.ProContractDetail;
import top.infopub.model.bpa.ProContractInvest;
import top.infopub.model.bpa.ProContractSub;
import top.infopub.model.bpa.ProContractUla;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.sg.CitySurvey;
import top.infopub.security.util.SessionUserUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class ProContractServiceImpl extends GenericServiceImpl<ProContract, String> implements ProContractService {

    @Autowired
    private ProContractDao proContractDao;

    @Autowired
    private OrgInfoDao orgInfoDao;
    
    @Autowired
    private CitySurveyDao citySurveyDao;

    @Autowired
    private WsFlowProcessService wsFlowProcess;

    @Autowired
    private ProContractInvestDao proContractInvestDao;

    @Autowired
    private ProContractDetailDao proContractDetailDao;

    @Autowired
    private ProContractSubDao proContractSubDao;

    @Autowired
    private ProContractAttDao proContractAttDao;

    @Autowired
    private ProContractUlaDao proContractUlaDao;

    @Override
    public GenericDao<ProContract, String> getDao() {
        return proContractDao;
    }

    @Override
    public Page<ProContractDomain> selectByDomainWhere(Page<ProContractDomain> page,
                                                       ProContractVo proContractVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProContractDomain>)proContractDao.selectByDomainWhere(proContractVo);

    }

    @Override
    public ProContractVo queryById(String id) {
        ProContract proContract = proContractDao.selectByPrimaryKey(id);
        ProContractVo proContractVo = new ProContractVo();
        BeanUtils.copyProperties(proContract, proContractVo);
        // 项目公司名，区域公司名处理
        if (StringUtils.isNotBlank(proContract.getHrOrgProId())) {
            OrgInfo orgInfo = orgInfoDao.selectByPrimaryKey(proContract.getHrOrgProId());
            if(orgInfo != null){
                proContractVo.setOrgProName(orgInfo.getCompName());
            }
        }
        if (StringUtils.isNotBlank(proContract.getHrOrgAreaId())) {
            OrgInfo orgInfo = orgInfoDao.selectByPrimaryKey(proContract.getHrOrgAreaId());
            if(orgInfo != null){
                proContractVo.setOrgAreName(orgInfo.getCompName());
            }
        }
        if (StringUtils.isNotBlank(proContract.getSgCityId())) {
            CitySurvey citySurvey = citySurveyDao.selectByPrimaryKey(proContract.getSgCityId());
            if(citySurvey != null){
                proContractVo.setSgCityName(citySurvey.getCityName());
            }
        }
        return proContractVo;
    }

    @Override
    public Page<ProContractDomain> selectMyCheckByDomainWhere(Page<ProContractDomain> page,
                                                              ProContractVo proContractVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProContractDomain>)proContractDao.selectMyCheckByDomainWhere(proContractVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveProContract(ProContract proContract, ProContractInvest proContractInvest,
                                List<ProContractUla> proContractUlas,
                                List<ProContractSub> proContractSubs,
                                List<ProContractDetail> proContractDetails,
                                List<ProContractAtt> proContractAtts) {
        //执行新增主表
        proContractDao.insertById(proContract);

        //新增合同投资信息
        proContractInvestDao.insertById(proContractInvest);

        //新增公式信息
        if (CollectionUtils.isNotEmpty(proContractUlas)) {
            proContractUlaDao.insertByBatch(proContractUlas);
        }

        //新增合同清单分解子项信息
        if (CollectionUtils.isNotEmpty(proContractSubs)) {
            proContractSubDao.insertByBatch(proContractSubs);
        }

        //新增合同清单分解子项费用明细
        if (CollectionUtils.isNotEmpty(proContractDetails)) {
            proContractDetailDao.insertByBatch(proContractDetails);
        }

        //新增附件
        if (CollectionUtils.isNotEmpty(proContractAtts)) {
            proContractAttDao.insertByBatch(proContractAtts);
        }

        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.BPA_PRO_CONTRACT;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, proContract.getId(), proContract.getProcessId(), null);

    }

    /**
     * 查询项目合同信息 并且项目没有发起过继承协议
     */
    @Override
    public List<ProContract> selectAllUsableProContract(String ids, String proCode) {
        return proContractDao.selectAllUsableProContract(ids, proCode,
            wsFlowProcess.proxyVisibleProCodes());
    }

    /**
     * 根据项目编号，查询项目合同信息
     */
    @Override
    public ProContractDomain selectByContractProCode(String proCode) {
        return proContractDao.selectByContractProCode(proCode);
    }

    @Override
    public List<ProContract> isExistContractName(String contractName,String proCode) {
        return proContractDao.isExistContractName(contractName,proCode);
    }

    @Override
    public ProContract selectContractSignDate() {
        return proContractDao.selectContractSignDate();
    }

    @Override
    public List<ProContract> selectAllContracts(String ids, String proCode) {
        return proContractDao.selectAllContracts(ids, proCode,
            wsFlowProcess.proxyVisibleProCodes());
    }

    @Override
    public List<ProContract> selectProContracts(String ids, String proCode) {
        return proContractDao.selectProContracts(ids, proCode);
    }

    @Override
    public ProContractVo queryByIds(String id) {
        return proContractDao.queryById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sign(ProContractVo proContractVo, List<ProContractAtt> proContractAtts) {
        ProContract proContract = new ProContract();
        proContract.setId(proContractVo.getId());
        proContract.setSignDate(proContractVo.getSignDate());
        proContract.setUptBy(SessionUserUtils.getSessionUserId());
        proContract.setUptTime(DateUtils.getCurrentDateTime());
        //更新记录
        proContractDao.updateByIdSelective(proContract);

        //批量插入附件
        if (CollectionUtils.isNotEmpty(proContractAtts)) {
            proContractAttDao.insertByBatch(proContractAtts);
        }
    }

    @Override
    public Page<BpastContractDomain> selectBpastByDomainWhere(Page<BpastContractDomain> page,String type, String orgId,
        String proName,String extType,String contractName) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Map<String, Object> where = new HashMap<String, Object>();
        where.put("proName", proName);
        where.put("contractName", contractName);
        where.put("extType", extType);
        if(type.equals("2")){//区域公司
            where.put("hrOrgAreaId", orgId);
        }else if(type.equals("3")){//项目公司
            where.put("hrOrgProId", orgId);
        }
        return (Page<BpastContractDomain>)proContractDao.selectBpastByDomainWhere(where);
    }

    @Override
    public ProContract isExistProName(String proCode) {
        return proContractDao.isExistProName(proCode);
    }

}